<?php

namespace app\api\model;

use think\Model;

/**
 * 媒体基础分类
 * @author 01
 * @email 319204520@qq.com
 * @date 2023-02-12 22:00
 */
class BaseCategory extends Model
{

    protected $name = 'jq_base_cate';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    public function updateParentCate($category) {

        // 存在启用的一级启用
        $info = $this->where(['parent_id' => $category['parent_id'], 'status' => 1])->where('is_delete', '0')->count();
        if ($info > 0)  {
            $this->where('id', $category['parent_id'])->where('is_delete', '0')->update(['status' => 1]);
        } else {
            // 不存在一级禁用
            $this->where('id', $category['parent_id'])->where('is_delete', '0')->update(['status' => 0]);
        }

    }

    public function updateParentCateWarin($category) {
        // 全警示 一级警示
        $info = $this->where(['parent_id' => $category['parent_id'], 'iswarn' => 0])->where('is_delete', '0')->count();
        if ($info <= 0)  {
            $this->where('id', $category['parent_id'])->where('is_delete', '0')->update(['iswarn' => 1]);
        } else {
            // 否则不警示
            $this->where('id', $category['parent_id'])->where('is_delete', '0')->update(['iswarn' => 0]);
        }

    }

    public function getIdByList($ids)
    {

        $map = [];
        $map['id'] = ['in', $ids];
        $map['level'] = '1';

        $id_list = $this->where($map)
            ->field('parent_id as first_cateid,id as second_cateid')
            ->select();

        return $id_list;
    }

}
